home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 1146 / 1146.xpi / chrome / screengrab.jar / content / File.js < prev    next >
Text File  |  2009-03-09  |  5KB  |  133 lines

  1.  
  2. screengrab.File = function(file, mimeType) {
  3.     this.nsFile = file;
  4.     this.mimeType = mimeType;
  5. }
  6. screengrab.File.FileInputStream = Components.Constructor("@mozilla.org/network/file-input-stream;1", "nsIFileInputStream", "init");
  7. screengrab.File.BinaryInputStream = Components.Constructor("@mozilla.org/binaryinputstream;1", "nsIBinaryInputStream", "setInputStream");
  8. screengrab.File.MultiplexInputStream = Components.Constructor("@mozilla.org/io/multiplex-input-stream;1", "nsIMultiplexInputStream");
  9. screengrab.File.BufferedInputStream = Components.Constructor("@mozilla.org/network/buffered-input-stream;1", "nsIBufferedInputStream", "init");
  10. screengrab.File.StringInputStream = Components.Constructor("@mozilla.org/io/string-input-stream;1", "nsIStringInputStream", "setData");
  11. screengrab.File.IOService = Components.Constructor("@mozilla.org/network/io-service;1", "nsIIOService");
  12. screengrab.File.prototype = {
  13.     remove : function() {
  14.         this.nsFile.remove(false);
  15.     },
  16.     
  17.     getBufferedStream: function() {
  18.         var stream = new sg.File.FileInputStream(this.nsFile, 0x01, 00004, null);
  19.         return new sg.File.BufferedInputStream(stream, 90000);
  20.     },
  21.     
  22.     readBytes: function() {
  23.         // open the local file
  24.         var stream = new sg.File.FileInputStream(this.nsFile, 0x01, 00004, null);
  25.         var buffered = new sg.File.BufferedInputStream(stream, 90000);
  26.         var binary = new sg.File.BinaryInputStream(buffered);
  27.         var data = "";
  28.         while (binary.available() > 0) {
  29.             data += binary.readBytes(binary.available());
  30.         }
  31.         return data;
  32.     },
  33.     
  34.     toFileUrl : function() {
  35.         return new sg.File.IOService().newFileURI(this.nsFile);
  36.         
  37.     },
  38.     saveDataUrlWithCallbackWhenDone : function(dataUrl, callback) {
  39.         var nsUri = new sg.File.IOService().newURI(dataUrl, "UTF8", null);
  40.         
  41.         var persist = this.createPersist();
  42.         persist.progressListener = {
  43.             onStateChange : function(aWebProgress, aRequest, aStateFlags, aStatus) {
  44.                 if ((aStateFlags & Components.interfaces.nsIWebProgressListener.STATE_STOP) == Components.interfaces.nsIWebProgressListener.STATE_STOP) {
  45.                     screengrab.debug("Saved.");
  46.                     if (callback) callback();
  47.                 }
  48.             }
  49.         };
  50.         persist.saveURI(nsUri, null, null, null, null, this.nsFile);
  51.     },
  52.     
  53.     saveDataUrl: function(dataUrl, quietly){
  54.         sg.debug("Saving...");
  55.         var nsUri = new sg.File.IOService().newURI(dataUrl, "UTF8", null);
  56.         
  57.         var persist = this.createPersist();
  58.         if (!quietly) {
  59.             persist.progressListener = this.createTransferProgressListener(nsUri, persist);
  60.         }
  61.         persist.saveURI(nsUri, null, null, null, null, this.nsFile);
  62.     },
  63.     
  64.     createPersist: function() {
  65.         var persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].createInstance(Components.interfaces.nsIWebBrowserPersist);
  66.         persist.persistFlags = Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
  67.         persist.persistFlags |= Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
  68.         return persist;
  69.     },
  70.     
  71.     createTransferProgressListener: function(nsUri, persist) {
  72.         var target = this.toFileUrl();
  73.         var tr = Components.classes["@mozilla.org/transfer;1"].createInstance(Components.interfaces.nsITransfer);
  74.         tr.init(nsUri, target, "", null, null, null, persist);
  75.         return tr;
  76.     }
  77. }
  78. screengrab.File.newTempFile = function(name, mimeType) {
  79.     var file = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("TmpD", Components.interfaces.nsIFile);
  80.     file.append(name);
  81.     file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0666);
  82.     return new screengrab.File(file, mimeType);
  83. }
  84. screengrab.File.named = function(name, mimeType) {
  85.     var nsFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
  86.     nsFile.initWithPath(name);
  87.     return new screengrab.File(nsFile, mimeType);
  88. }
  89. screengrab.File.sanitiseName = function(name) {
  90.     var replaceChar = "_";
  91.     var fileNameRegEx = new RegExp('[,/\:*?""<>|]', 'g');
  92.     return name.replace(fileNameRegEx, replaceChar);
  93. }
  94.  
  95. screengrab.ImageFilePicker = function(defaultFileName, message, selected) {
  96.     this.defaultFileName = defaultFileName;
  97.     this.message = message;
  98.     this.selected = selected;
  99.     this.file = null;
  100.     this.type = null;
  101. }
  102. screengrab.ImageFilePicker.prototype = {
  103.     show : function() {
  104.         var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(Components.interfaces.nsIFilePicker);
  105.         
  106.         fp.init(window, this.message, Components.interfaces.nsIFilePicker.modeSave);
  107.         fp.defaultString = this.defaultFileName;
  108.         fp.appendFilter("PNG", "*.png;*.gnp");
  109.         fp.appendFilter("JPG", "*.jpg;*jpeg");
  110.         if (this.selected == "image/png") {
  111.             fp.filterIndex = 0;
  112.         } else {
  113.             fp.filterIndex = 1;
  114.         }
  115.         
  116.         var result = fp.show();
  117.         if (result == fp.returnOK || result == fp.returnReplace) {
  118.             this.file =  fp.file;
  119.             this.type = "image/png";
  120.             this.mimeOptions = "";
  121.             if (fp.filterIndex == 1) {
  122.                 this.type = "image/jpeg";
  123.             }
  124.         }
  125.     },
  126.     
  127.     getFile : function() {
  128.         if (this.file == null) {
  129.             this.show();
  130.         }
  131.         return new screengrab.File(this.file, this.type);
  132.     }
  133. }